%-------------------------------------------------------------------------
% load data from disk files
% 
% @param-input
%   m_dataset       a unique string represent different data set 
%   m_channels      [1..maxchannels]
%   m_timebegin     seconds
%   m_timeend       seconds
% @param-output
%   m_block         containing the final data matrix for further analysis
% 
% @attention
%   the current working directory may be changed after execute this script
% @example
%   m_dataset = 'ecog-01';
%   m_channels = [1,2,8,9,10,11,12,23];
%   m_timebegin = 18802-100;
%   m_timeend = 18802+100;
%   spt_loaddata;
%   do with m_block now...
%
% @modified by zhangwei on 200802
%   test passed
%-------------------------------------------------------------------------
            
% assert: current dir is D:\dev\sciexplorer\source\experiment;

if (~exist('m_channels')), m_channels = 0, end
if ~exist('m_timebegin'), m_timebegin = 0, end;
if (~exist('m_timeend'))
    error('spt_loaddata: parameter m_timeend not defined!');
end

%-------------------------------------------------------------------------
% load data
%-------------------------------------------------------------------------

% possible choices: 
%   ecog-01, ecog-02, eeg-bonn-xx, eeg-caltech-xx, eeg-kul-xx, eeglab-xx
%
% recommended: ecog-01, ecog-02, eeg-caltech-xx
%
m_block = [];
m_metadata.name = 'm_block';
m_metadata.description = '';
m_metadata.samplerate = 250;
m_metadata.file = '';
m_metadata.timestart = 0;
m_metadata.timeend = 0;

switch m_dataset
    % ecog from mayo clinic: 24 channels
    % assert: current working directory is: D:\dev\sciexplorer\source\experiment\
    case {'ecog-01'}
        samplerate = 100;
        period = 5;
        prefix = './ecog-01(seg)/segment';
        surfix = '';
        m_timeend;
        m_block = fun_block_from_segments( m_timebegin, m_timeend, samplerate, prefix, surfix, m_channels ); 
        %eegplot( m_block' );
        disp( ['load dataset ' m_dataset ' successfully. data size = [' num2str(size(m_block)) ']'] );

    % ecog from mayo clinic: 1 channels
	case 'ecog-02'
        %fun_block_from_ecog2( timebegin, timeend, channels );
        datadir = 'D:\dev\sciexplorer\source\experiment\ecog-02\';
        datafile = 'grid22.mef';
        samplerate = 500;
        blocksize = 3600 * samplerate;  % 3600 sec = an hour
        
        fid = fopen( [datadir, datafile], 'r' );
        [m_block, count] = fread( fid, blocksize*25, 'int32', blocksize*0+1 );
        disp( ['successfully read ' num2str(count)] );
        fclose(fid);
        %save .\block.mat m_block
        %load .\block.mat
        disp( ['load dataset ' m_dataset ' successfully. data size = [' num2str(size(m_block)) ']'] );
        
    case 'eeg-kul-f1'
        cd D:\dev\sciexplorer\source\experiment\
        channel = 0;
        samplerate = 250;
        prefix = './eeg-kul(seg)/f1_segment';
        surfix = 'hr';
        % prefix = './eeg-kul(seg)/f34_segment';
        % surfix = 'hr';
        m_timebegin;
        m_timeend;
        m_block = fun_block_from_segments( m_timebegin, m_timeend, samplerate, prefix, surfix, m_channels ); 
        % eegplot( m_block' );
        disp( ['load dataset ' m_dataset ' successfully. data size = [' num2str(size(m_block)) ']'] );

    case 'eeg-kul-f34'
        cd D:\dev\sciexplorer\source\experiment\
        channel = 0;
        samplerate = 250;
        prefix = './eeg-kul(seg)/f34_segment';
        surfix = 'hr';
        m_timebegin;
        m_timeend;
        m_block = fun_block_from_segments( m_timebegin, m_timeend, samplerate, prefix, surfix, m_channels ); 
        % eegplot( m_block' );
        disp( ['load dataset ' m_dataset ' successfully. data size = [' num2str(size(m_block)) ']'] );
    
    % i'm not sure whether the data read out is right or wrong
    case 'eeg-caltech'
        [filename, pathname] = uigetfile('*.Ncs','Select file');
        cd(pathname);
        if length(filename) == 8
            channel = filename(4);
        else
            channel = filename(4:5);
        end

        f=fopen(filename,'r','l');
        fseek(f,16384,'bof');                                     %Skip Header, put pointer to the first record
        TimeStamps=fread(f,inf,'int64',(4+4+4+2*512));            %Read all TimeStamps
        fseek(f,16384+8+4+4+4,'bof');                             %put pointer to the beginning of data
        time0 = TimeStamps(1); 
        timeend = TimeStamps(end);
        delta_time=(TimeStamps(2)-TimeStamps(1));
        clear TimeStamps;

        sr = 512*1e6/delta_time;
        % LOAD CSC DATA (for plotting)
        fseek(f,16384+8+4+4+4,'bof');                               %put pointer to the beginning of data
        Samples=fread(f,ceil(sr*60),'512*int16=>int16',8+4+4+4);  
        x=double(Samples(:))';
        clear Samples; 
        fclose(f);

        %GETS THE GAIN AND CONVERTS THE DATA TO MICRO V.
        eval(['scale_factor=textread(''CSC' num2str(channel) '.Ncs'',''%s'',41);']);
        m_block = x*str2num(scale_factor{41})*1e6;
        eegplot( m_block );
        disp( ['load dataset ' m_dataset ' successfully. data size = [' num2str(size(m_block)) ']'] );
end
        
% @post
% now the variable m_block contains the data you want 
